import copy
from conf.db import get_db

db = get_db()


class EscResourceAide:
    cfg = [
        {'label': '执行资源名称', 'field': 'name', 'required': True},
        {'label': '连接类型', 'field': 'connect_type', 'required': True},
        {'label': 'IP地址', 'field': 'ip_address', 'required': True},
        {'label': '端口', 'field': 'port', 'required': True},
        {'label': '资源说明', 'field': 'remark', 'required': True},
        {'label': '验证登录类型', 'field': 'login_type', 'required': True},
        {'label': '登录账号', 'field': 'account', 'required': True},
    ]

    @classmethod
    def edit_cfg(cls):
        cfg = copy.deepcopy(cls.cfg)
        cfg.append({'label': '登录密码', 'field': 'password', 'required': True})
        # cfg.append({'label': '公钥路径', 'field': 'public_key_path', 'required': False})
        # cfg.append({'label': '私钥路径', 'field': 'private_key_path', 'required': False})
        for i in cfg:
            i['type'] = 'text'
            if 'connect_type' == i['field']:
                i['type'] = 'select'
                i['options'] = [{'name': 'SSH', 'value': '1'}]
            if 'login_type' == i['field']:
                i['type'] = 'select'
                i['options'] = [{'name': '密码', 'value': 1}, {'name': '密钥', 'value': 2}]
            if 'remark' == i['field']:
                i['type'] = 'textarea'
        return cfg

    @classmethod
    def resource_list_fmt(cls, cfg, data):
        for one in cfg:
            for row in data:
                value = row.get(one['field'])
                if one['field'] in ['connect_type']:
                    _map = {1: 'ssh'}
                    row[one['field']] = _map.get(value)
                if one['field'] in ['login_type']:
                    _map = {1: '密码', 2: '密钥'}
                    row[one['field']] = _map.get(value)
